package com.jjc.android.fragment

import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.jjc.android.R
import com.jjc.android.activity.CameraActivity
import com.jjc.android.activity.DetectionActivity
import com.jjc.android.activity.DetectionReportActivity
import com.jjc.android.base.BaseLazyLoadFragment
import com.jjc.android.view.layout.onClick
import com.jjc.android.view.onClick

class SamplingTakePhotoFragment : BaseLazyLoadFragment() {

    companion object{
        fun newInstance():SamplingTakePhotoFragment{
            val fragment = SamplingTakePhotoFragment()
            fragment.setInflateLazy(true)
            return fragment
        }
    }

    private lateinit var failerLayout: LinearLayout
    private lateinit var reTakeBtn: TextView
    private lateinit var toTakeLayout: LinearLayout
    private lateinit var okBtn: TextView
    private lateinit var takePhotoImageView: ImageView

    override fun getLayoutId(): Int = R.layout.fragment_sampling_take_photo

    override fun initView(view: View) {
        super.initView(view)
        initViews(view)
        val action:()->Unit = {
            showTakeLayout()
            (activity as? DetectionActivity)?.let { activity->
                CameraActivity.start(activity,activity.getDetectionInfo()){detectionId->
                    if(detectionId.isNullOrEmpty()){
                        showFailureLayout()
                    }else{
                        DetectionReportActivity.start(requireContext(),detectionId)
                        activity.finish()
                    }
                }
            }
        }
        reTakeBtn.onClick(action = action)
        okBtn.onClick (action = action)

    }
    private fun initViews(view: View) {
        takePhotoImageView = view.findViewById(R.id.takePhotoImageView)
        okBtn = view.findViewById(R.id.okBtn)
        toTakeLayout = view.findViewById(R.id.toTakeLayout)
        reTakeBtn = view.findViewById(R.id.reTakeBtn)
        failerLayout = view.findViewById(R.id.failerLayout)
        showTakeLayout()
    }

    private fun showFailureLayout(){
        toTakeLayout.visibility = View.GONE
        failerLayout.visibility = View.VISIBLE
        playGif(false)
    }

    private fun showTakeLayout(){
        toTakeLayout.visibility = View.VISIBLE
        failerLayout.visibility = View.GONE
        playGif(true)
    }

    private fun playGif(isPlay:Boolean){
        if(isPlay){
            Glide
                .with(this)
                .asGif()
                .load(R.mipmap.insert_card_gif_icon)
                .into(takePhotoImageView)
        }else{
            if (takePhotoImageView.drawable is GifDrawable) {
                val drawable = takePhotoImageView.drawable as GifDrawable
                if (drawable.isRunning) {
                    drawable.stop()
                }
//                else {
//                    drawable.start()
//                }
            }
        }
    }

}